/**
 * Created with IntelliJ IDEA.
 * Description :
 * User: $ {USER}
 * Date: $ {YEAR}-$ {MONTH}一$ { DAY}
 * Time: ${ TIME}
 */

/**
 * 题目：删除排序链表中的重复元素 II
 * 思路：遍历链表，统计每一个出现的次数，将只出现一次的结点相连接，组成的链表就是最终结果
 */
public class Test {
    public ListNode deleteDuplicates(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        // 遍历链表，用数组来存储其结点值
        int[] count = new int[201];
        ListNode cur = head;
        while (cur != null) {
            count[cur.val+100]++;
            cur = cur.next;
        }
        // 开始构建链表
        ListNode newHead = new ListNode(-1);
        ListNode newCur = newHead;
        cur = head;
        while (cur != null) {
            // count等于1就说明这个只能出现一次，因此没必要对其进行处理
            if (count[cur.val+100] == 1) {
                ListNode newNode = new ListNode(cur.val);
                newCur.next = newNode;
                newCur = newCur.next;
            }
            cur = cur.next;
        }
        return newHead.next;
    }
}

class ListNode {
    int val;
    ListNode next;

    ListNode() {
    }

    ListNode(int val) {
        this.val = val;
    }

    ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }
}